package com.itttq.gen.generator;

import com.alibaba.druid.sql.SQLUtils;
import com.alibaba.druid.util.JdbcConstants;
import com.google.common.collect.Lists;
import com.itttq.gen.common.constant.Constants;
import com.itttq.gen.generator.to.GeneratorTo;
import com.itttq.gen.generator.to.TableTo;
import com.itttq.gen.model.po.ProjectPo;
import com.itttq.gen.utils.DbUtil;
import com.itttq.gen.utils.StringUtil;

import java.util.Collections;
import java.util.List;

public class TableInfo {

    /**
     * 获取表信息
     * @param project
     * @return
     */
    public static List<GeneratorTo> getTableInfo(ProjectPo project) {
        List<TableTo> tableTos;
        switch (project.getSourceType()) {
            case DB:
                tableTos = DbUtil.getTableInfoFromDb(project);
                break;
            case SQL:
                project.setSqlStatement(SQLUtils.format(project.getSqlStatement(), JdbcConstants.MYSQL));
                tableTos = DbUtil.getTableInfoFromSql(project.getSqlStatement());
                break;
            case SQL_FILE:
                // TODO 文件的方式暂时不支持，只是保留
                tableTos = DbUtil.getTableInfoFromSql(project.getSqlStatement());
                break;
            default:
                tableTos = Lists.newArrayList();
        }
        List<GeneratorTo> generatorTos = Lists.newArrayList();
        tableTos.forEach(tableTo -> {
            GeneratorTo.GeneratorToBuilder generatorBuilder = GeneratorTo.builder();
            // 转换表名为驼峰命名格式
            String humpTn = StringUtil.hump4Underline(tableTo.getName());
            // 首字母大写
            String capTn = StringUtil.capitalize(humpTn);
            generatorBuilder.swaggerEnable(project.getSwaggerEnable()).remark(tableTo.getRemark()).alias(humpTn)
                    .aliasCapFirst(capTn).tableName(tableTo.getName())
                    // 设置PO相关参数
                    .poName(capTn + "Po").poSerialVersionUID(StringUtil.genRandomNum(3) + System.nanoTime() / 10)
                    .poPkg((project.getPackagePath() + ".model.po").replace(Constants.SLASH, Constants.DOT))
                    .qoName(capTn + "Qo").qoPkg((project.getPackagePath() + ".model.qo").replace(Constants.SLASH, Constants.DOT))
                    .toPkg((project.getPackagePath() + ".model.to").replace(Constants.SLASH, Constants.DOT))
                    .mapperName("I" + capTn + "Mapper").mapperPkg((project.getPackagePath() + ".mapper").replace(Constants.SLASH, Constants.DOT))
                    .serviceName(capTn + "Service").servicePkg((project.getPackagePath() + ".service").replace(Constants.SLASH, Constants.DOT))
                    .controllerName(capTn + "Controller").controllerPkg((project.getPackagePath() + ".controller").replace(Constants.SLASH, Constants.DOT))
                    .controllerSerialVersionUID(StringUtil.genRandomNum(3) + System.nanoTime() / 10)
                    .primaryKey(tableTo.getPrimaryKey()).primaryKeyJavaType(tableTo.getPrimaryKeyJavaType())
                    .fields(tableTo.getColumns())
            ;
            generatorTos.add(generatorBuilder.build());
        });

        // 进行排序处理
        Collections.sort(generatorTos);
        return generatorTos;
    }
}
